package com.xii.eduservice.controller;


import com.xii.commonutils.R;
import com.xii.eduservice.entity.EduChapter;
import com.xii.eduservice.entity.chapter.ChapterVo;
import com.xii.eduservice.service.EduChapterService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author xhjava
 * @since 2022-02-13
 */
@RestController
@Api(description = "章节管理")
@RequestMapping("/eduservice/educhapter")
//@CrossOrigin
public class EduChapterController {

    @Autowired
    private EduChapterService eduChapterService;
    /**
     * @description: 根据id获得嵌套的数据列表
     *
     * @author  wangxihao
     * @email wangxh0108@163.com
    **/
    @ApiOperation(value = "嵌套章节数据列表")
    @GetMapping("nestedlist/{courseId}")
    public R nestedListByCourseId(
            @ApiParam(name = "courseId", value = "课程ID", required = true)
            @PathVariable String courseId){    //请求过来的参数
        List<ChapterVo> chapterVoList = eduChapterService.nestedList(courseId);
        return R.ok().data("items", chapterVoList);
    }


    @ApiOperation(value = "添加章节")
    @PostMapping("addChapter")
    public R addChapter(@RequestBody EduChapter chapter){
        eduChapterService.save(chapter);
        return R.ok();
    }

    /**
     * @description: 根据id查询章节
     *
     * @author  wangxihao
     * @email wangxh0108@163.com
    **/
    @ApiOperation(value = "根据id查询")
    @GetMapping("getChapterInfoById/{chapterId}")
    public R getChapter(@PathVariable String chapterId){
        EduChapter byId = eduChapterService.getById(chapterId);
        return R.ok().data("chapter",byId);
    }

    /**
     * @description: 修改章节
     *
     * @author  wangxihao
     * @email wangxh0108@163.com
    **/
    @ApiOperation(value = "修改章节")
    @PostMapping("updateChapter")
    public R updateChapter(@RequestBody EduChapter eduChapter){
        eduChapterService.updateById(eduChapter);
        return R.ok();
    }

    /**
     * @description: 根据id进行删除
     *
     * @author  wangxihao
     * @email wangxh0108@163.com
    **/
    @ApiOperation(value = "根据id进行删除")
    @DeleteMapping("removeChapterById/{ChapterId}")
    public R removeChapter(@PathVariable String ChapterId){
        //删除章节的时候 如果里面有小节 不让他删除
        boolean b = eduChapterService.deleteChapter(ChapterId);
        if(b){
            return R.ok();
        }else {
            return R.error();
        }
    }

}

